<template>
  <div>
    <div class="app-container pageHeader">
      <div class="header">{{$route.meta.title}}</div>
    </div>

    <div class="app-container margin-content padding-content">
      <el-form
        :model="dataForm"
        ref="dataForm"
        label-width="120px">
        <el-row :gutter="48">
          <el-col :sm="24" :md="8">
            <el-form-item label="报关（检）单号" prop="declNo">
              <el-input placeholder="报关（检）单号" v-model="dataForm.declNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="提运单号" prop="blNo">
              <el-input placeholder="提运单号" v-model="dataForm.blNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="集装箱号" prop="contNo">
              <el-input placeholder="集装箱号" v-model="dataForm.contNo" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8" v-if="isAdmin">
            <el-form-item label="申报口岸" prop="portId">
              <treeselect
                placeholder="请选择"
                :options="options"
                :disable-branch-nodes="true"
                value-consists-of="LEAF_PRIORITY"
                :close-on-select="true"
                :open-on-click="true"
                v-model="dataForm.portId"/>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="查验下达时间" prop="time">
              <el-date-picker
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                unlink-panels
                value-format="yyyy-MM-dd"
                v-model="dataForm.time"
                :picker-options="pickerOptions"
                style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :sm="24" :md="8">
            <el-form-item label="查验结果" prop="resType">
              <el-select change-on-select v-model="dataForm.resType" style="width: 100%;">
                <el-option v-for="item in resType"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <div style="text-align: right">
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>

    <div class="app-container margin-content padding-content">
      <el-table
        :key="tableKey"
        :data="tableData"
        v-loading="loading"
        element-loading-text="正在加载..."
        :highlight-current-row="true"
        style="width: 100%;"
        :max-height="scrollHeight">

        <el-table-column label="报关（检）单号" property="declNo" width="150" fixed="left"></el-table-column>
        <el-table-column label="查验方式" header-align="center">
          <el-table-column label="查验来源" property="checkSource" width="120">
            <template slot-scope="scope">
              {{scope.row.checkSource | typeFilter}}
            </template>
          </el-table-column>

          <el-table-column label="提运单号" property="blNo" width="150"></el-table-column>
          <el-table-column label="集装箱号" property="contNo" width="120"></el-table-column>
          <el-table-column label="进出口标识" property="ieFlag" width="100">
            <template slot-scope="scope">
              {{scope.row.ieFlag | ieFlagFilter}}
            </template>
          </el-table-column>
          <el-table-column label="申报口岸" property="declPort" width="120">
            <template slot-scope="scope">
              {{scope.row.declPort | portCodeFilter}}
            </template>
          </el-table-column>
          <el-table-column label="查验方式" property="checkType" width="90">
            <template slot-scope="scope">
              {{scope.row.checkType.charAt(0) | checkTypeFilter}}
            </template>
          </el-table-column>
          <el-table-column label="查验下达时间" property="checkTime" width="140"></el-table-column>
          <el-table-column label="查验结果" property="resType" width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.resType === '0'" type="danger">
                {{scope.row.resType | resTypeFilter}}
              </el-tag>
              <el-tag v-else-if="scope.row.resType === '1'" type="success">
                {{scope.row.resType | resTypeFilter}}
              </el-tag>
              <el-tag v-else type="info">暂无信息</el-tag>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="补贴统计" header-align="center" property="readies">
          <el-table-column label="吊装费" property="liftFee" width="90"/>
          <el-table-column label="移位费" property="moveFee" width="90"/>
          <el-table-column label="仓储费" property="storeFee" width="90"/>
          <el-table-column label="补贴合计" property="totalFee" width="90"/>
          <el-table-column label="查验准备" width="90">
            <template slot-scope="scope">
              <el-popover
                placement="left"
                title="明细"
                trigger="click"
                v-if="scope.row.readies.length > 0">
                <el-table :data="scope.row.readies" max-height="150">
                  <el-table-column label="码头操作类型" property="actType" width="120">
                    <template slot-scope="ready">
                      {{ready.row.actType | chkReadyFilter}}
                    </template>
                  </el-table-column>
                  <el-table-column label="集装箱位置" property="location" width="120">
                    <template slot-scope="ready">
                      {{ready.row.location | locationFilter}}
                    </template>
                  </el-table-column>
                  <el-table-column label="准备时间" property="readyTime" width="130"/>
                </el-table>
                <el-tag
                  slot="reference"
                  type="info">查看
                </el-tag>
              </el-popover>
              <el-tag
                type="info"
                v-else>暂无信息
              </el-tag>
            </template>
          </el-table-column>
        </el-table-column>

        <!--<el-table-column label="放行指令" header-align="center">-->
        <el-table-column label="放行下达时间" property="releaseTime" width="130" fixed="right"></el-table-column>
        <!--</el-table-column>-->
        <el-table-column label="操作" :width="hasPermission([0,269]) ? 150 : 0" fixed="right">
          <template slot-scope="scope">
            <a @click="view(scope.row.id)">查看</a>
            <span v-permission="[0,269]">
              <divider type="vertical"></divider>
              <a @click="orderReady(scope.row)">查验准备录入</a>
            </span>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container align-right" v-show="tableData.length > 0">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="dataForm.pageNum"
          :page-sizes="[10,20,30,50]"
          :page-size="dataForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>

    <!--查验准备录入-->
    <chk-dialog v-if="chkDialogVisible" ref="chkOrder" :is-fee="1"></chk-dialog>
  </div>
</template>

<script>
  // api
  import { findChkOrderCosts } from '@/api/chkOrder'
  import { getPortTree } from '@/api/port'
  // utils
  import { pickerOptions, getLately } from '@/utils'
  import { portCode, resType } from '@/utils/selecterOptions'
  import { modifyObj } from '@/utils/tools'
  import { hasPermission } from '@/utils/permission' // 权限判断函数
  // vuex
  import { mapMutations, mapGetters } from 'vuex'
  // components
  import Divider from '@/components/Divider'
  import Treeselect from '@riophae/vue-treeselect'
  import ChkDialog from '@/components/ChkDialog'

  import permission from '@/directive/permission/index.js' // 权限判断指令

  const time = getLately(7)

  export default {
    name: 'chk-fee-manage-list',
    directives: {
      permission
    },
    data() {
      return {
        tableKey: 0,
        tableData: [],
        loading: false,
        scrollHeight: null,
        isAdmin: this.$store.getters.isAdmin,
        dataForm: {
          time: time,
          declNo: '',
          blNo: '',
          contNo: '',
          portId: null,
          resType: '',
          pageNum: 1,
          pageSize: 20
        },
        total: 0,
        pickerOptions: {
          shortcuts: pickerOptions
        },
        portCode: portCode,
        resType: resType,
        options: [],
        chkDialogVisible: false
      }
    },
    activated() {
      if (this.$route.params && this.$route.params.trackType) {
        this.dataForm.contNo = ''
        this.dataForm.blNo = ''
        if (this.$route.params.trackType === '0') {
          this.dataForm.contNo = this.$route.params.recordNo
          this.dataForm.time = [this.$route.params.checkTime, this.$route.params.checkTime]
        } else {
          this.dataForm.blNo = this.$route.params.recordNo
          this.dataForm.time = [this.$route.params.checkTime, this.$route.params.checkTime]
        }
        this._fetchList()
      }
    },
    created() {
      this._getPortTree()
      this._findChkOrderCosts()
      this.tableResize()
    },
    methods: {
      tableResize() {
        const _height = 420
        this.scrollHeight = document.body.scrollHeight - _height
        window.onresize = () => {
          this.scrollHeight = document.body.scrollHeight - _height
        }
      },
      _getPortTree() {
        getPortTree().then((res) => {
          this.options = modifyObj(res.data.data)
        })
      },
      _findChkOrderCosts() {
        this.loading = true
        this.tableData = []

        const [beginTime, endTime] = this.dataForm.time || ''
        const params = { beginTime, endTime }
        const data = Object.assign({}, params, this.dataForm)

        findChkOrderCosts(data).then((res) => {
          this.tableData = res.data.data.dataList
          this.total = res.data.data.total
          this.loading = false
        })
      },
      view(id) {
        this.$parent.changeView(1)
        this.setCurrentId(id)
      },
      orderReady(data) {
        this.chkDialogVisible = true
        this.$nextTick(() => {
          this.$refs['chkOrder'].init(data)
        })
      },
      resetForm() {
        this.dataForm = {
          time: time,
          declNo: '',
          blNo: '',
          contNo: '',
          portId: null,
          resType: '',
          pageNum: 1,
          pageSize: 20
        }
      },
      handleSizeChange(val) {
        this.dataForm.pageSize = val
        this._findChkOrderCosts()
      },
      handleCurrentChange(val) {
        this.dataForm.pageNum = val
        this._findChkOrderCosts()
      },
      handleSearch() {
        this.dataForm.pageNum = 1
        this._findChkOrderCosts()
      },
      hasPermission,
      ...mapMutations({
        setCurrentId: 'SET_CURRENTID'
      })
    },
    components: {
      Divider,
      Treeselect,
      ChkDialog
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss">
</style>
